import {
  getCarBaoXianList,
  getCarBaoxianInfo
} from '../../../utils/networkRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
        id: '0',
        title: '全部'
      },
      {
        id: '1',
        title: '商业90天内'
      },
      {
        id: '2',
        title: '交强90天内'
      }
    ],
    selectedId: '0',
    carList: [],
    page: 1,
    last_page: -1, //最后一页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getCarBaoXianList(this.data.selectedId,this.data.page, (r) => {
      console.log(r);
      if (r.code == 0) {
        self.setData({
          carList: r.data,
          last_page: r.pager.last_page
        })
      }
    })
  },
  handleTabChange: function(e) {

    let self = this;
    debugger;
    getCarBaoXianList(e.detail,1, (r) => { 
      if (r.code == 0) {
        self.setData({
          carList: r.data,
          page:1,
          selectedId: e.detail,
          last_page: r.pager.last_page
        })
      }
    })
  },
  //下啦刷新
  onPullDownRefresh: function () {
    let self = this;
    let {
      last_page,
      selectedId,
    } = this.data;
    getCarBaoXianList(selectedId, 1, (r) => {
      self.setData({
        carList: r.data,
        page: 1,
        last_page: r.pager.last_page
      })
      wx.stopPullDownRefresh()
    })
  },
  //上拉加载
  onReachBottom: function () {
    var self = this;
    let {
      page,
      last_page,
      selectedId,
    } = this.data;

    if (last_page <= page) {
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page = page + 1;
      getCarBaoXianList(selectedId, page, (r) => {
        console.log(r); 
        self.setData({
          carList: self.data.carList.concat(r.data),
          page: page,
          last_page: r.pager.last_page
        })
      })
    }

  },
  //查看详情
  goDetail: function(e) {
    let carNumber = e.currentTarget.dataset.carnumber;
    getCarBaoxianInfo(carNumber, (r) => {
      if (r.code == 0) {
        console.log(r)
        wx.navigateTo({
          url: '../baoxiandetail/baoxiandetail?dataInfo=' + JSON.stringify(r.data),
        })
      } else {
        wx.showToast({
          icon: "none",
          title: r.message,
        })
      }
    })
  },
  goSearch: function() {
    wx.redirectTo({
      url: "../usersearch/usersearch"
    })
  },
  goList: function() {
    wx.redirectTo({
      url: "../userlist/userlist"
    })
  }
})